package com.ayz.gateway.client.connection;



import com.ayz.gateway.client.ServerInfo;
import com.ayz.gateway.client.exception.ExecuteRequestException;
import com.ayz.gateway.client.executor.RequestExecutorRegistry;
import com.ayz.gateway.client.executor.ResponseCallback;

import java.util.concurrent.TimeoutException;

/**
 * @ClassName Connection
 * @Description TODO: 客户端与服务器的连接
 * @Author 快乐的星球
 * @Date 2023/5/20 12:43
 * @Version 1.0
 **/
public interface Connection {
    /**
     * 获取连接唯一标识
     * 默认是 “timestamp_serverIp_serverPort”
     * @return
     */
    public String getConnectionId();

    /**
     * 获取服务端的信息
     * @return
     */
    public ServerInfo getServerInfo();

    /**
     * 获取请求执行器注册器
     * @return
     */
    public RequestExecutorRegistry getRequestExecutorRegistry();

    /**
     * 初始化连接
     */
    public void start();

    /**
     * 关闭连接
     */
    public void close();

    /**
     * 执行请求
     * @param requestCode 请求唯一编码
     * @param request     请求对象
     * @param timeoutMills 超时时间
     * @return
     */
    public Object request(int requestCode,Object request,long timeoutMills)throws TimeoutException, ExecuteRequestException;

    /**
     * 发起请求
     * @param requestCode 请求唯一编码
     * @param request     请求对象
     * @param responseCallback  响应的回调函数
     */
    public void asyncRequest(int requestCode, Object request, ResponseCallback responseCallback)throws TimeoutException, ExecuteRequestException;

    /**
     * 向服务端发起一次请求，不需要响应
     * @param requestCode
     * @param request
     * @throws ExecuteRequestException
     */
    public void requestOneway(int requestCode,Object request) throws ExecuteRequestException;
}
